home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / 2499.ZIP / TIMEIT.ZIP / TIMEIT.C < prev    next >
Text File  |  1989-02-15  |  4KB  |  123 lines

  1.  
  2. /** TIMEIT.c *********************************************************/
  3. /*              WINDOWS PROGRAM TIMER APPLICATION
  4.                          by: Steve Burg
  5. */
  6. /*********************************************************************/
  7.  
  8. #define NOMINMAX
  9.  
  10. #include <windows.h>    /* all Windows functions */
  11. #include <dde.h>
  12. #include <stdlib.h>    /* itoa */
  13. #include <string.h>    /* strcat & strlen */
  14.  
  15. ATOM aAppl;
  16.  
  17. long FAR PASCAL WndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam);
  18.  
  19. /* WinMain - Main loop for all Windows applications */
  20. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  21.                     int nCmdShow)
  22. {
  23.    static   char szAppName[] = "Windows Timeit";
  24.    WNDCLASS WndClass;
  25.    HWND     hWnd;
  26.    MSG      msg;
  27.  
  28.    if (!hPrevInstance)
  29.    {
  30.       /* define Window class */
  31.       lpszCmdLine[0] = '\0';      /* Remove Compiler warnings */
  32.       nCmdShow = 0;
  33.       WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  34.       WndClass.hIcon         = NULL;
  35.       WndClass.cbClsExtra    = 0;
  36.       WndClass.cbWndExtra    = 0;
  37.       WndClass.lpszMenuName  = NULL;
  38.       WndClass.lpszClassName = (LPSTR) szAppName;
  39.       WndClass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
  40.       WndClass.hInstance     = hInstance;
  41.       WndClass.style         = CS_VREDRAW | CS_HREDRAW;
  42.       WndClass.lpfnWndProc   = WndProc;
  43.       /* register this new class with WINDOWS */
  44.       if (!RegisterClass((LPWNDCLASS)&WndClass))
  45.          return FALSE;   /* Initialization failed */
  46.    }
  47.  
  48.    /* create window */
  49.    hWnd = CreateWindow((LPSTR) szAppName, (LPSTR)  szAppName,
  50.             WS_TILEDWINDOW,
  51.             0, 0, 0, 0,
  52.             (HWND)NULL, (HMENU)NULL,
  53.                         (HANDLE)hInstance, (LPSTR)NULL);
  54.  
  55.    /* show window (start in ICONIC state) */
  56.    ShowWindow(hWnd, SHOW_ICONWINDOW);
  57.    UpdateWindow(hWnd);
  58.  
  59.    aAppl = GlobalAddAtom(szAppName);
  60.  
  61.    /* main program loop */
  62.    while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  63.    {
  64.       TranslateMessage((LPMSG) &msg);
  65.       DispatchMessage((LPMSG) &msg);
  66.    } /* end while GetMessage */
  67.  
  68.    GlobalDeleteAtom(aAppl);
  69.    /* exit back to Windows */
  70.    return (int)msg.wParam;
  71. } /* end WinMain */
  72.  
  73. /* WndProc - Window procedure for main window */
  74. long FAR PASCAL WndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  75. {
  76.    char buffer[20], buf1[10];
  77.    PAINTSTRUCT ps;
  78.    RECT    rect;
  79.    DWORD  dwEndTime;
  80.    static DWORD dwTime;
  81.    static WORD wSec = 0, wTenthSec = 0;
  82.    static HWND hWndDDE;
  83.  
  84.    /* switch on message type sent to window by Windows dispatcher */
  85.    switch(message)
  86.    {
  87.       case WM_DDE_INITIATE:
  88.          if (LOWORD(lParam) == aAppl)
  89.          {
  90.             GlobalDeleteAtom(aAppl);
  91.             hWndDDE = wParam;
  92.             dwTime = GetCurrentTime();
  93.          }
  94.          break;
  95.       case WM_DDE_TERMINATE:
  96.          if (wParam == hWndDDE)
  97.          {
  98.             dwEndTime = GetCurrentTime();
  99.             dwEndTime -= dwTime;
  100.             wSec = (WORD)(dwEndTime/1000);
  101.             wTenthSec = (WORD)((dwEndTime/100)%10);
  102.             InvalidateRect(hWnd, NULL, TRUE);
  103.          }
  104.          break;
  105.       case WM_PAINT:
  106.          BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  107.      GetClientRect(hWnd, (LPRECT) &rect);
  108.      DrawText(ps.hdc, (LPSTR)buffer,
  109.             strlen(strcat(strcat(strcat(itoa(wSec, buffer, 10), "."),
  110.             itoa(wTenthSec, buf1, 10)), " Sec.")),
  111.             (LPRECT)&rect, DT_WORDBREAK);
  112.          EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  113.          break;
  114.       case WM_DESTROY:
  115.          PostQuitMessage(0);
  116.          break;
  117.       default:
  118.          return DefWindowProc(hWnd, message, wParam, lParam);
  119.    } /* end switch */
  120.  
  121.    return 0L;
  122. } /* end WndProc */
  123.